import pygame

def setMode(game, console, mode):

  '''\
  Call game.setMode
  Use $modes for avaliable modes
  Parameters:
          mode - int of game mode
  '''

  game.setMode(mode)

def setQuality(game, console, quality):

  '''\
  Set game.common.settings.quality
  Parameters:
          mode - int 1-3. 1 is lowest, 3 is highest
  '''

  game.common.settings.quality = quality

def setFPSCap(game, console, fps):

  '''\
  Set game.common.fps
  Parameters:
          fps - int frames per second cap
  '''
  game.common.fps = fps


def quitGame(game, console):

  '''\
  Quit the game
  '''
  game.quit = 1

def musicloop(game, console, loop):

  '''\
  Call game.music.loadQueue and game.music.playQueue
  May not work in all modes. use $musicloops to see avaliable loops
  Parameters:
          loop - str name of loop from sounds.ini
  '''
  game.music.loadQueue(loop)
  game.music.playQueue()

def loadSave(game, console, saveslot):

  '''\
  Call game.loadSave
  Parameters:
          saveslot - int of saveslot
  '''
  game.loadSave(str(saveslot))

def save(game, console):

  '''\
  Call game.saveWorld
  Saves currently loaded world
  '''

  try:
    game.saveWorld(game.world.saveslot)
  except:
    return 'World not initiated yet'


def setObjAttribute(game, console, name, attribute, value):

  '''\
  Set an object's attributes
  Parameters:
          name - name of object
          attribute - attribute to set
          value - value to set attribute
  '''

  try:
    if game.world.objecthandler.getObject(name):
      obj = game.world.objecthandler.getObject(name)
      obj.attributes[attribute] = value
    else:
      return 'No object named "' + name + '"'
  except:
    return 'World not initiated yet'


def getObjAttribute(game, console, name, attribute):

  '''\
  Get an object's attribute
  Parameters:
          name - name of object
          attribute - attribute to set
  '''
  try:
    if game.world.objecthandler.getObject(name):
      return game.world.objecthandler.getObject(name).attributes[attribute]
    else:
      return 'No object named "' + name + '"'
  except:
    return 'World not initiated yet'


def getObjAttributes(game, console, name):

  '''\
  Get an object's attributes dictionary
  Warning, this could be long
  Parameters:
          name - name of object
  '''
  try:
    if game.world.objecthandler.getObject(name):
      return list(game.world.objecthandler.getObject(name).attributes.keys())
    else:
      return 'No object named "' + name + '"'
  except:
    return 'World not initiated yet'

def getObjects(game, console):

  '''\
  Get list of all currently loaded objects
  (object-name, object-type)
  '''
  try:
    objs = []
    for obj in game.world.objecthandler.objects:
      objs.append((obj.name, obj.type))
    return objs
  except:
    return 'World not initiated yet'

def getObjectsByAttribute(game, console, attribute, value):

  '''\
  Get list of all objects with an attribute set to the given valaue
  Parameters:
          attribute - Name of attribute to check
          value - expected value
  '''
  try:
    objs = []
    for obj in game.world.objecthandler.objects:
      if attribute in obj.attributes:
        if obj.attributes[attribute] == value:
          objs.append(obj.name)
    return objs
  except:
    return 'World not initiated yet'


def setGameTime(game, console, time):

  '''\
  Set game time to given time
  Parameters:
          time - Time in 0:00 format to set time to. Needs to be wrapped in single quotes.
  '''
  try:
    if convertTime(time):
      game.world.gametime = convertTime(time)
    else:
      return 'Invalid time'
  except:
    return 'World not initiated yet'

#-------------------------------------------------------------------------------
#--------------------------- Game specific -------------------------------------
#-------------------------------------------------------------------------------

def setSystem(game, console, system, value):

  '''\
  Set a computer system
  Parameters:
          ssytem - name of system to change
          value - value to set it to
  '''

  try:
    if system in game.world.computer.status:
      game.world.computer.status[system] = value
    else:
      return 'No system named ' + system
  except:
    return 'World not initiated yet'

#-------------------------------------------------------------------------------
#--------------------------- Function Dict -------------------------------------
#-------------------------------------------------------------------------------

# Add any functions above here. The key is the command used in console, the value is the
# function above. General engine functions can be added to 'enginefuncs', anything game
# specific for example, the rocketeer's ship computer, should be added to 'gamefuncs'.

enginefuncs ={"mode":setMode, "quality":setQuality, "fpscap":setFPSCap, \
            "quit":quitGame, 'musicloop':musicloop, "loadsave":loadSave, \
            "save":save, 'setobjattr':setObjAttribute, 'getobjattr':getObjAttribute, \
            "getobjattrs":getObjAttributes, 'objects':getObjects, 'getobjsbyattr':getObjectsByAttribute, \
            "gametime":setGameTime}

gamefuncs = {"setsystem":setSystem}

# Put the two dicts together before it is passed off to the console
functions = dict(enginefuncs.items() + gamefuncs.items())


#-------------------------------------------------------------------------------
#---------------------------------- Utils --------------------------------------
#-------------------------------------------------------------------------------

def convertTime(time):

  """ Convert a string time in 00:00 format to integer seconds """
  try:
    time = time[1:-1]
    split = time.split(':')

    converted = 0
    #check that there was actually a minute supplied
    if split[0]:
      converted = int(split[0]) * 60

    converted += int(split[1])

    return converted
  except:
    return None


